class Counter2 {
    private int count = 0;
    void add(){
        synchronized(this) {
            count++;
        }
    }
    int getCount(){
        return count;
    }
}
public class Demo21 {
    public static void main(String[] args) throws InterruptedException {
        Counter2 counter2 = new Counter2();
        Thread thread = new Thread(() -> {
            for(int i = 0; i < 50000; i++){
                counter2.add();
            }
        });
        Thread thread2 = new Thread(() -> {
           for(int i = 0; i < 50000; i++){
               //synchronized属于可重入锁，会记录下每一次加锁的对象，如果是同一个线程加的同一把锁，则不会出现死锁、
               // 但是c++和python属于不可重入锁，会出现死锁现象
               synchronized(counter2) {
                   counter2.add();
               }
           }
        });
        thread.start();
        thread2.start();
        thread.join();
        thread2.join();
        System.out.println("count = " + counter2.getCount());
    }
}
